package com.esiran.payadmin.controller;

import com.esiran.payadmin.entity.Resp;
import com.esiran.payadmin.exception.APIRespException;
import com.esiran.payadmin.exception.AuthenticationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class ExceptionHandlerController {
    @ExceptionHandler(AuthenticationException.class)
    public void authenticationException(
            AuthenticationException e,
            HttpServletRequest request,
            HttpServletResponse httpServletResponse) throws IOException {
        if (e.getRedirect() != null){
            httpServletResponse.sendRedirect(e.getRedirect());
        }
    }
    @ExceptionHandler(APIRespException.class)
    @ResponseBody
    public Resp<Void> apiResultException(APIRespException e) {
        return Resp.create(e.getCode(),null,e.getMessage());
    }
}
